package com.example.safehome;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.litepal.LitePal;

public class CoDetectorActivity extends AppCompatActivity {

    CoDetector coDetector;
    CoDetectorDB coDetectordb;
    TextView textViewSystemResult, textViewCoResult, textViewCoLevel, textViewExanswer, textViewPlace,textViewCabinResult,textViewRecord;
    LinearLayout llCoPlace, llMessage;
    String str="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.codetector_activity_main);
        textViewSystemResult = (TextView)  findViewById(R.id.tv_system_result);
        textViewCoResult = (TextView) findViewById(R.id.tv_co_result);
        textViewCoLevel = (TextView) findViewById(R.id.tv_co_level);
        textViewExanswer = (TextView) findViewById(R.id.tv_exanswer);
        textViewPlace = (TextView) findViewById(R.id.tv_place);
        textViewCabinResult = (TextView) findViewById(R.id.tv_cabin_result);
        textViewRecord=(TextView) findViewById(R.id.tv_record);
        llCoPlace = (LinearLayout) findViewById(R.id.ll_co_place);
        llMessage = (LinearLayout) findViewById(R.id.ll_message);
        SQLiteDatabase db = LitePal.getDatabase();
        coDetector = new CoDetector();
        coDetectordb = new CoDetectorDB();

        // 数据库初始化
        coDetectordb.setCoLevel(0.00);
        coDetectordb.setExceedPosition("kitchen");
        coDetectordb.setIsRunning(false);
        coDetectordb.setSystemStatus(true);
        coDetectordb.save();
        textViewCabinResult.setText("0");
        str=str+"时间"+"                                                  "+"地点"+"\n";
    }

    //按钮“运行”的点击事件
    public void run(View view) {
        coDetectordb.setIsRunning(true);
        coDetectordb.save();
        CoDetector coDetector = new CoDetector();
        coDetector.isRunning = true;
        textViewCoResult.setText("正常运行");
    }

    //按钮“退出”的点击事件
    public void exit(View view) {
        coDetectordb.setIsRunning(false);
        coDetectordb.save();
        CoDetector coDetector = new CoDetector();
        coDetector.isRunning = false;
        textViewCoResult.setText("停止运行");
    }

    //按钮“开始检测”的点击事件
    public void detect(View view) {
        //变量从左至右分别代表系统运行状态，检测器运行状态，一氧化碳是否超标和一氧化碳超标地点的答案
        String systemStatus, detectorStatus, coState, coPlace;

        //变量分别代表一氧化碳报警标准和一氧化碳浓度
        double coStandard, coAnswer;

        // 显示系统状态
        boolean sysStatus = coDetectordb.getSystemStatus();
        if (sysStatus == true)
            systemStatus = "正常运行";
        else systemStatus = "停止运行";
        textViewSystemResult.setText(systemStatus);

        //显示检测器状态
        if (coDetectordb.getIsRunning())
            detectorStatus = "正常运行";
        else detectorStatus = "停止运行";
        textViewCoResult.setText(detectorStatus);

        //显示一氧化碳浓度标准
        coStandard = coDetector.standard;
        textViewCoLevel.setText(Double.toString(coStandard));

        //显示一氧化碳浓度
        coAnswer = coDetectordb.getCoLevel();
        textViewCabinResult.setText(Double.toString(coAnswer));


        //显示是否超标
        if (isExceed()){
            coState = "超标";
            llCoPlace.setVisibility(View.VISIBLE);
            llMessage.setVisibility(View.VISIBLE);
            coPlace=coDetectordb.getExceedPosition();
            textViewPlace.setText(coPlace);
            coDetector.ringAlarm(coDetector.coLevel,coDetector.standard);

            //显示co泄露记录
            //获取当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date date = new Date(java.lang.System.currentTimeMillis());
            str=str+simpleDateFormat.format(date)+"       "+coDetectordb.getExceedPosition()+"\n";
            textViewRecord.setText(str);
        }
        else {
            coState = "未超标";
        }
        textViewExanswer.setText(coState);
    }

    //一氧化碳检测是否超标
    public boolean isExceed() {
        double coLevel = coDetectordb.getCoLevel();
        if (coLevel >= coDetector.standard)
            return true;
        else return false;
    }

    //按钮“打电话”的点击事件
    public void Call(View view) {
        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+"18567687972"));
        startActivity(intent);
    }

    //按钮“发短信”的点击事件
    public void sendMessage(View view) {
        Uri smsToUri = Uri.parse("smsto:18567687972");
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", "您家里方式一氧化碳泄露，请迅速处理");
        startActivity(intent);
    }

    public void coDetectorBack(View view) {
        finish();
    }

    public void Test(View view) {
        textViewRecord.setVisibility(View.INVISIBLE);
        coDetectordb.setCoLevel(256.00);
        detect(view);
    }

    public void Query(View view){
        textViewRecord.setVisibility(View.VISIBLE);
    }
}